package leetcode_100;



/**
 *@author 周杨
 *SpiralMatrixII_59 旋转填充数组 用规律
 *describe: AC 100%
 *2018年5月9日 下午9:53:52
 */
public class SpiralMatrixII_59 {
	int [][]nums;
	public static void main(String[] args) {
		SpiralMatrixII_59 test=new SpiralMatrixII_59();
		int [][] res=test.generateMatrix(0);
		test.print();

	}
	
	public int[][] generateMatrix(int n) {
        this.nums=new int[n][n];
        int count=(n+1)/2;
        int now=0;
        int mount=1;
        while(now<count) {
        	for(int i=now;i<n-now;++i) {
        		nums[now][i]=mount;
        		++mount;
        	}
        	for(int i=now+1;i<n-now;++i) {
        		nums[i][n-now-1]=mount;
        		++mount;
        	}
        	for(int i=n-now-2;i>=now;--i) {
        		nums[n-now-1][i]=mount;
        		++mount;
        	}
        	for(int i=n-now-2;i>now;--i) {
        		nums[i][now]=mount;
        		++mount;
        	}
        	++now;
        }
        return this.nums;
    }
	
	public void print() {
		for(int i=0;i<nums.length;++i) {
			for(int j=0;j<nums.length;++j) {
				System.out.print(nums[i][j]+" ");
			}
			System.out.println();
		}
	}

}
